#!/bin/bash

# Slackware build script for scid

# written by Marco Pessotto <melmothx@gmail.com>
# public domain

# Modified by Robby Workman <rworkman@slackbuilds.org>

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=scid
VERSION=${VERSION:-4.3}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

# If the variable PRINT_PACKAGE_NAME is set, then this script will report what
# the name of the created package would be, and then exit. This information
# could be useful to other scripts.
if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM
tar xvf $CWD/$PRGNAM-$VERSION.tar.bz2
cd $PRGNAM
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
 -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

# Don't force the possibly wrong tcl version.
sed -i -e "s/tclsh8.5;/tclsh;/" configure

./configure \
  BINDIR="/usr/bin" \
  SHAREDIR="/usr/share/scid" \
  OPTIMIZE="$SLKCFLAGS -fno-rtti -fno-exceptions"

make all_scid
make strip
make install_scid DESTDIR=$PKG

## Add some stuff if it's present
if [ -f $CWD/scidlet40k.zip ] ; then
    mkdir -p $PKG/usr/share/scid/books
    unzip $CWD/scidlet40k.zip -d $PKG/usr/share/scid/books
fi
if [ -f $CWD/ratings_2008_4.zip ] ; then
    mkdir -p $PKG/usr/share/scid/data
    unzip $CWD/ratings_2008_4.zip -d $PKG/usr/share/scid/data
fi
if [ -f $CWD/photos2007.zip ] ; then
    mkdir -p $PKG/usr/share/scid/photos
    unzip $CWD/photos2007.zip -d $PKG/usr/share/scid/photos
fi

# Fix some permissions
find $PKG/usr/share/scid -type d -exec chmod 0755 {} \;
find $PKG/usr/share/scid -type f -exec chmod 0644 {} \;

# We're going to diverge from upstream a bit in location but not functionality
# This will require users to be in the 'games' group for write permissions
mkdir -p $PKG/var/games/scid
mv $PKG/usr/share/scid/{bases,books} $PKG/var/games/scid
chown -R root:games $PKG/var/games/scid
find $PKG/var/games/scid -type d -exec chmod 2775 {} \;
find $PKG/var/games/scid -type f -exec chmod 0664 {} \;
cd $PKG/usr/share/scid
  ln -s ../../../var/games/scid/bases .
  ln -s ../../../var/games/scid/books .
cd -

# Add a desktop menu entry
mkdir -p $PKG/usr/share/{applications,pixmaps}
cat $CWD/scid.desktop > $PKG/usr/share/applications/scid.desktop
cat $CWD/scid.png > $PKG/usr/share/pixmaps/scid.png

## Add the manpage (Thanks to debian)
mkdir -p $PKG/usr/man/man1
cd $PKG/usr/man/man1
  sed "s%@VERSION@%$VERSION%g" $CWD/scid.1 | gzip -9c > scid.1.gz
  for i in \
    sc_remote scmerge pgnscid sc_spell spliteco sc_eco sc_tree scidpgn pgnfix \
    tkscid sc_epgn sc_addmove tcscid sc_import scidlet scidt ;
    do ln -s scid.1.gz $i.1.gz
  done
cd -

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
  ChangeLog CHANGES COPYING help README THANKS TODO \
    $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
